/**
 * @file lv_korean_ime.c
 *
 */

/*********************
 *      INCLUDES
 *********************/
#include "lv_korean_ime.h"
#include <stdio.h>

/*********************
 *      DEFINES
 *********************/
#define MY_CLASS    &lv_100ask_korean_ime_class

uint16_t py_page;           /* Current pinyin map pages */
uint16_t py_num[27];        /* Number and length of Pinyin */  //第26个存储shift 开头的 偏移和数量
uint16_t py_pos[27];        /* Pinyin position */


/**********************
 *      TYPEDEFS
 **********************/

/**********************
 *  STATIC PROTOTYPES
 **********************/

static lv_100ask_dict_t * korean_search_matching_dict(lv_obj_t * obj, char * strInput_py_str, uint16_t * cand_num);
/**********************
 *  STATIC VARIABLES
 **********************/

                                                

#define LV_100ASK_korean_IME_DEFAULT_PIN_YIN_DICT 1
#if LV_100ASK_korean_IME_DEFAULT_PIN_YIN_DICT
lv_100ask_dict_t lv_100ask_def_korean_dict[] = {
            {"a", "ㅁ", ""},  
            {"b", "ㅛ", ""},  
            {"c", "ㅍ", ""},  
            {"d", "ㅇ", ""},  
            {"e", "ㄷ", ""},  
            {"f", "ㄹ", ""},  
            {"g", "ㅋ", ""},  
            {"h", "ㅣ", ""},  
            {"j", "ㅡ", ""},  
            {"k", "ㅏ", ""},  
            {"l", "ㅜ", ""},  
            {"o", "ㅗ", ""},  
            {"p", "ㅓ", ""},  
            {"q", "ㅂ", ""},  
            {"r", "ㄱ", ""},  
            {"s", "ㄴ", ""},  
            {"t", "ㅎ", ""},  
            {"v", "ㅠ", ""},  
            {"w", "ㅈ", ""},  
            {"x", "ㅊ", ""},  
            {"z", "ㅌ", ""}, 
            {"Se", "ㄸ", ""},  
            {"Sf", "ㅆ", ""},  
            {"Sh", "ㅟ", ""},  
            {"Sk", "ㅑ", ""},  
            {"Sp", "ㅕ", ""},  
            {"Sq", "ㅃ", ""},  
            {"Sr", "ㄲ", ""},  
            {"Sw", "ㅉ", ""},  
            {NULL, NULL}
};
#endif





void korean_input_proc(lv_obj_t * obj)
{
    lv_100ask_pinyin_ime_t * korean_ime = (lv_100ask_pinyin_ime_t *)obj;
    printf("=====korean_input_proc====\n");
    printf("korean_ime->input_char:%s\n",korean_ime->input_char);
    //处理shift
    char*pp = strstr(korean_ime->input_char,KEYPAD_SHIFT);
    if(pp)
    {
        printf("origin:%s\n",pp);
        memset(pp,0,5);
        pp[0]='S';
        //存在Shift
        for(int i=pp-korean_ime->input_char[0]+5;i<sizeof(korean_ime->input_char);i++)
        {
            pp[i-4]=pp[i];
        }
        printf("changed:%s\n",pp);
    }
    lv_100ask_dict_t *cand_dict = korean_search_matching_dict(obj, korean_ime->input_char, &korean_ime->cand_num);
    if (cand_dict == NULL) 
    {
        return;
    }
    printf("dict:%s.%s,%s\n",cand_dict->py,cand_dict->py_mb,cand_dict->py_hz);
    korean_ime->py_page = 0;
printf("=====korean_input_proc2====\n");
    for(uint8_t i = 0; i < LV_100ASK_PINYIN_IME_CAND_TEXT_NUM; i++)
    {
        printf("=====loop %d====\n",i);
        memset(korean_ime->btnm_pinyin_sel_user[i], 0x00, CAND_STR_MAX_LEN);
        korean_ime->btnm_pinyin_sel_user[i][0] = ' ';
    }
    printf("=====korean_input_proc3====\n");
    char* p = cand_dict->py_hz;
    // fill buf
    if(strlen(cand_dict->py_mb)<=CAND_STR_MAX_LEN)
        strcpy(korean_ime->btnm_pinyin_sel_user[0],cand_dict->py_mb);
    else
        strncpy(korean_ime->btnm_pinyin_sel_user[0],cand_dict->py_mb,CAND_STR_MAX_LEN);
    int len = strlen(cand_dict->py_hz);
    int candeindex=1;
    printf("=====korean_input_proc4====\n");
    while(candeindex<LV_100ASK_PINYIN_IME_CAND_TEXT_NUM)
    {
        char*q = strchr(p,' ');
        if(q && (q-p>0) )
        {
            int substr_len = q-p;
            strncpy(korean_ime->btnm_pinyin_sel_user[candeindex],p,substr_len);
            korean_ime->btnm_pinyin_sel_user[candeindex][substr_len]='\0';
            candeindex++;
            p = q+1;

        }
        else{
            //处理最后一个候选
            if(p &&strlen(p)>0)
            {
                strncpy(korean_ime->btnm_pinyin_sel_user[candeindex],p,strlen(p));
                korean_ime->btnm_pinyin_sel_user[candeindex][strlen(p)]='\0';
                candeindex++;
            }
            break;
        }
    }
    printf("=====korean_input_proc5====\n");
    //for(uint8_t i = 0; (i < korean_ime->cand_num && i < LV_100ASK_korean_IME_CAND_TEXT_NUM); i++)
    lv_obj_clear_flag(korean_ime->cand_panel, LV_OBJ_FLAG_HIDDEN);
}


void init_korean_dict(lv_obj_t * obj, lv_100ask_dict_t * dict)
{
    char headletter = 'a';
    uint16_t offset_sum = 0;
    uint16_t offset_count = 0;
    uint16_t letter_calc = 0;

    for(uint16_t i = 0; ; i++)
    {
        if ((NULL == (dict[i].py)) || (NULL == (dict[i].py_mb)))
        {
            headletter = dict[i-1].py[0];
            //letter_calc = headletter - 'a';
            if(headletter == 'S')
            {
                //处理shift
                letter_calc = 26;
            }
            else{
                letter_calc = headletter - 'a';
            }

            py_num[letter_calc] = offset_count;
            break;
        }   
        if (headletter == (dict[i].py[0]))
        {
            offset_count++;
        }
        else
        {
            headletter = dict[i].py[0];
            if(dict[i].py[0] == 'S')
            {
                //处理shift
                letter_calc = 26;
            }
            else{
                letter_calc = headletter - 'a';
            }
            py_num[letter_calc - 1] = offset_count;
            offset_sum += offset_count;
            py_pos[letter_calc] = offset_sum;

            offset_count = 1;
        }       
    }
}

static lv_100ask_dict_t * korean_search_matching_dict(lv_obj_t * obj, char * strInput_py_str, uint16_t * cand_num)
{
    lv_100ask_pinyin_ime_t * korean_ime = (lv_100ask_pinyin_ime_t *)obj;
    lv_100ask_dict_t * cpHZ;
    uint8_t i, cInputStrLength = 0, offset;
    volatile uint8_t count = 0;

    if(*strInput_py_str == '\0')    return NULL;
    if(*strInput_py_str == 'i')     return NULL;
    if(*strInput_py_str == 'u')     return NULL;
    if(*strInput_py_str == 'y')     return NULL;
    if(strInput_py_str[0] == 'S') //处理shift
        offset = 26;
    else
        offset = strInput_py_str[0] - 'a';
    cInputStrLength = strlen(strInput_py_str);
    
    cpHZ  = &lv_100ask_def_korean_dict[py_pos[offset]];
    count = py_num[offset];

    while(count--)
    {
        for(i = 0; i < cInputStrLength; i++)
        {
            if(*(strInput_py_str + i) != *((cpHZ->py) + i))
            {
                break;
            }
        }
        // perfect match
        if(cInputStrLength == 1 || i == cInputStrLength)
        {
            // The Chinese character in UTF-8 encoding format is 3 bytes
            //* cand_num = 1;//strlen((const char*)(cpHZ->py_mb)) / 3;
            //第二列+第三列
            * cand_num = 1;
            return cpHZ;
        }       
        cpHZ++;
    }
    return NULL;
}
